package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;


    @Override
    public List<CategoryVo> getBaseCategoryList() {
        return baseCategory1Mapper.getBaseCategoryList();
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.findAttribute(category1Id);
    }

    @Override
    public BaseCategoryView getAlbumCategory(Long albumId) {
        return baseCategoryViewMapper.getAlbumCategory(albumId);
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long c1Id) {
        // 1.根据一级分类id 查询该一级分类的二级分类集合

        LambdaQueryWrapper<BaseCategory2> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(BaseCategory2::getCategory1Id, c1Id);
        wrapper1.eq(BaseCategory2::getIsDeleted, 0);
        List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(wrapper1);
        if (CollectionUtils.isEmpty(baseCategory2s)) {
            throw new GuiguException(201, "该一级分类下不存在二级分类集合");
        }

        // 2.过滤二级分类的id
        List<Long> baseCategory2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());


        // 3.查询三级分类
        LambdaQueryWrapper<BaseCategory3> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(BaseCategory3::getCategory2Id, baseCategory2Ids);
        wrapper2.eq(BaseCategory3::getIsTop, 1);  // 置顶展示的三级分类对象
        wrapper2.eq(BaseCategory3::getIsDeleted, 0);
        wrapper2.last("limit 7");
        List<BaseCategory3> category3s = baseCategory3Mapper.selectList(wrapper2);
        return category3s;
    }

    @Override
    public CategoryVo getBaseCategoryListByC1Id(Long c1Id) {
        return baseCategory1Mapper.getBaseCategoryListByC1Id(c1Id);
    }

    @Override
    public List<Long> getAllCategory1Id() {

        List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
        List<Long> c1Ids = baseCategory1s.stream().map(baseCategory1 -> baseCategory1.getId()).collect(Collectors.toList());
        return c1Ids;
    }
}

